package com.hwj.controller;


import cn.hutool.jwt.JWTUtil;
import com.hwj.entity.Likes;
import com.hwj.service.LikesService;
import com.hwj.utils.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/likes")
@Slf4j
public class LikesController {

    @Autowired
    LikesService likesService;
    /*
    * 点赞和取消
    * */
    @PostMapping("/set")
    public JsonResponse set(@RequestBody Likes likes, HttpServletRequest request){
        String token = request.getHeader("token");
        Object userId = JWTUtil.parseToken(token).getPayload("userId");
        likes.setUserId(Long.parseLong(userId+""));
        boolean set = likesService.set(likes);
        return JsonResponse.successResponse(0,"点赞或取消赞操作成功",set);
    }

    /*
     * 查询点赞的状态
     * */
    @PostMapping("/flag")
    public JsonResponse flag(@RequestBody Likes likes, HttpServletRequest request){
        String token = request.getHeader("token");
        Object userId = JWTUtil.parseToken(token).getPayload("userId");
        likes.setUserId(Long.parseLong(userId+""));
        boolean set = likesService.flag(likes);
        return JsonResponse.successResponse(0,"查询点赞状态成功",set);
    }

}
